package com.pmsystem.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.BuildingMapper;
import com.pmsystem.pojo.Building;
import com.pmsystem.pojo.BuildingExample;
import com.pmsystem.pojo.BuildingShow;
import com.pmsystem.pojo.Village;
import com.pmsystem.pojo.BuildingExample.Criteria;

/**
 * 
* <p>Title: BuildingService</p>  
* <p>Description:处理大楼信息的业务层 </p>  
* @author 树昌  
* @date 2019年1月21日
 */
@Service
public class BuildingService {
	
	@Autowired
	BuildingMapper buildingMapper;
	@Autowired
	VillageService villageService;
	
	/**
	 * 
	 * <p>Title: addBuilding</p>  
	 * <p>Description:添加一条大楼信息的记录 </p>  
	 * @author 树昌
	 * @param building
	 * @return 
	 */
	public boolean addBuilding(Building building) {
		System.out.println(building.getBuildingName());
		int row = buildingMapper.insertSelective(building);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showBuilding</p>  
	 * <p>Description:查询所有大楼的所有信息 </p>  
	 * @author 树昌
	 * @return
	 */
	public List<BuildingShow> showBuilding(String keyword, Integer startRow, Integer pageSize,Integer VID) {
		BuildingExample buildingExample = new BuildingExample();
		Criteria criteria = buildingExample.createCriteria();
		criteria.andEnableEqualTo((byte) 1);
		//条件查询
		if(VID!=null) {
			criteria.andBlgVillageIdEqualTo(VID);
		}
		
		
		// 模糊查询
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			criteria.andBuildingNameLike(keyword);
			Criteria criteria2=buildingExample.or();
			criteria2.andResponsibleLike(keyword);
			Criteria criteria3=buildingExample.or();
			criteria3.andResTelephoneLike(keyword);
		}
		// 分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow-1)*pageSize) >= 0 && pageSize >= 1) {
				buildingExample.setStartRow((startRow-1)*pageSize);
				buildingExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		// 没有条件则为全查询
		List<Building> listbuilding = buildingMapper.selectByExample(buildingExample);
		List<Village> listvillage = villageService.showVillage(null, null, null);
		List<BuildingShow> listbuildingshow = new ArrayList<>();
		
		for (Building building : listbuilding) {
			
			BuildingShow buildingShow = new BuildingShow();
			buildingShow.setBuildingId(building.getBuildingId());
			buildingShow.setBuildingName(building.getBuildingName());
			
			if(building.getResponsible()!=null) {
				buildingShow.setResponsible(building.getResponsible());
			}else {
				buildingShow.setResponsible(" ");
			}
			if(building.getResTelephone()!=null) {
				buildingShow.setResTelephone(building.getResTelephone());
			}else {
				buildingShow.setResTelephone(" ");
			}
			
			for (Village village : listvillage) {
				
				if(building.getBlgVillageId().equals(village.getVillageId())) {
					buildingShow.setVillageName(village.getVillageName());
				}
			}
			
			listbuildingshow.add(buildingShow);
		}
		return listbuildingshow;
	}
	
	
	/**
	 * 
	 * <p>Title: showOneBuilding</p>  
	 * <p>Description:查询一条大楼的记录 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	public Building showOneBuilding(int id) {
		
		Building building = buildingMapper.selectByPrimaryKey(id);
		return building;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteBuilding</p>  
	 * <p>Description:删除一条大楼的记录 </p>  
	 * @author 树昌
	 * @param building
	 */
	public boolean deleteBuilding(Building building) {
		
		BuildingExample buildingExample = new BuildingExample();
		Criteria criteria = buildingExample.createCriteria();
		criteria.andBuildingIdEqualTo(building.getBuildingId());
		building.setEnable((byte) 0);
		
		int row = buildingMapper.updateByExampleSelective(building, buildingExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAllBuilding</p>  
	 * <p>Description:批量删除大楼记录 </p>  
	 * @author 树昌
	 * @param listID	要删除的记录的ID的集合
	 * @return
	 */
	public boolean deleteAllBuilding(List<Integer> listID) {
		
		BuildingExample buildingExample = new BuildingExample();
		Criteria criteria = buildingExample.createCriteria();
		criteria.andBuildingIdIn(listID);
		Building building = new Building();
		building.setEnable((byte) 0);
		
		int row = buildingMapper.updateByExampleSelective(building, buildingExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: updateBuilding</p>  
	 * <p>Description:通过ID修改1条大楼记录 </p>  
	 * @author 树昌
	 * @param building
	 */
	public boolean updateBuilding(Building building) {
		
		BuildingExample buildingExample = new BuildingExample();
		Criteria criteria = buildingExample.createCriteria();
		criteria.andBuildingIdEqualTo(building.getBuildingId());
		
		int row = buildingMapper.updateByExampleSelective(building, buildingExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteBuildingTableData</p>  
	 * <p>Description:清空大楼数据表 </p>  
	 * @author 树昌
	 * @return
	 */
	public boolean deleteBuildingTableData() {
		BuildingExample buildingExample = new BuildingExample();
		int row = buildingMapper.deleteByExample(buildingExample);
		return row==1?true:false;
	}
	
}
